home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / selectv1.tar / selectv1 / diskio.c < prev    next >
C/C++ Source or Header  |  1993-09-04  |  3KB  |  164 lines

  1. /*
  2. diskio.c: disk io procedures for selectnews
  3.  
  4. Copyright (C) 1993 Eugene Eric Kim
  5. All rights reserved.
  6.  
  7. LAST REVISION: September 4, 1993
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "headers.h"
  13.  
  14. void error(s)
  15. char* s;
  16. {
  17.   fprintf(stderr, s);
  18.   exit(1);
  19. }
  20.  
  21. FILE* GetFile()
  22. {
  23.   FILE *temp;
  24.  
  25.   if ( (temp = fopen(".newsrc", "r")) == NULL )
  26.     error("Cannot open .newsrc: make sure you are in your home directory.\n");
  27.   else
  28.     return temp;
  29. }
  30.  
  31. void Backup()
  32. {
  33.   FILE *from,*to;
  34.   char line[LEN];
  35.  
  36.   if ((from = fopen(".newsrc","r")) == NULL)
  37.     error("Cannot open .newsrc: make sure you are in your home directory");
  38.   if ((to = fopen(".newsrc.old","w")) == NULL)
  39.     error("Cannot write backup file.  Exiting.");
  40.   while (fgets(line,LEN,from) != NULL)
  41.     fputs(line,to);
  42.   close(from);
  43.   close(to);
  44. }
  45.  
  46. short validstring(str)
  47. char str[LEN];
  48. {
  49.   int i = 0;
  50.   short valid = 1, found = 0; /* found = 1 if ':' or = 2 if '!' */
  51.  
  52.   while (str[i]!='\n' && valid) {
  53.     if (!found) {
  54.       switch (str[i]) {
  55.       case ' ': case '\0':
  56.     valid = 0;
  57.     break;
  58.       case ':':
  59.     found = 1;
  60.     break;
  61.       case '!':
  62.     found = 2;
  63.     break;
  64.       default:
  65.     break;
  66.       }
  67.     }
  68.     else {
  69.       switch (str[i]) {
  70.       case '1': case '2': case '3': case '4': case '5': case '6':
  71.       case '7': case '8': case '9': case '0': case ' ': case '-':
  72.       case ',':
  73.     break;
  74.       default:
  75.     valid = 0;
  76.     break;
  77.       }
  78.     }
  79.     i++;
  80.   }
  81.   if ( (found == 1) && valid)
  82.     return 1;
  83.   else if ( (found == 2) && valid)
  84.     return 0;
  85.   else
  86.     return -1;
  87. }
  88.  
  89. void sortfile(f,b1,b2,i,j)
  90. FILE *f;
  91. dllist* b1;
  92. dllist* b2;
  93. int *i;
  94. int *j;
  95. {
  96.   char line[LEN];
  97.   short where;  /* 1 = sub; 0 = usub; -1 = trouble */
  98.   node* c1;
  99.   node* c2;
  100.  
  101.   (*i) = (*j) = 0;
  102.   c1 = list_start(*b1);
  103.   c2 = list_start(*b2);
  104.   while (fgets(line,LEN,f) != NULL) {
  105.     if (line[0] != '\n') {
  106.       where = validstring(line);
  107.       if (where == 1) {
  108.     c1 = list_insafter(b1,c1,line);
  109.     (*i)++;
  110.       }
  111.       else if (where == 0) {
  112.     c2 = list_insafter(b2,c2,line);
  113.     (*j)++;
  114.       }
  115.       else {
  116.     fprintf(stderr,"Error: .newsrc file corrupt at line %d:\n   ",*i + *j + 1);
  117.     fprintf(stderr,"%s\n",line);
  118.     exit(1);
  119.       }
  120.     }
  121.   }
  122.   fclose(f); /* close .newsrc */
  123. }
  124.  
  125. int Write(b1,b2)
  126. dllist b1;
  127. dllist b2;
  128. {
  129.   FILE *to;
  130.   int i;
  131.   node* c1;
  132.   node* c2;
  133.  
  134.   Backup();
  135.   if ( (to=fopen(".newsrc","w")) == NULL)
  136.     error("Can't write .newsrc. Exiting");
  137.   c1 = list_start(b1);
  138.   while (c1 != 0) {
  139.     i = 0;
  140.     while ( (*c1).entry[i]!='\0') {
  141.       if ( (*c1).entry[i]=='!')
  142.     putc(':',to);
  143.       else
  144.     putc((*c1).entry[i],to);
  145.       i++;
  146.     }
  147.     c1 = list_next(c1);
  148.   }
  149.   c2 = list_start(b2);
  150.   while (c2 != 0) {
  151.     i = 0;
  152.     while ( (*c2).entry[i]!='\0') {
  153.       if ( (*c2).entry[i]==':')
  154.     putc('!',to);
  155.       else
  156.     putc((*c2).entry[i],to);
  157.       i++;
  158.     }
  159.     c2 = list_next(c2);
  160.   }
  161.   fclose(to);
  162.   return 1;
  163. }
  164.